import request from '../utils/request';
import { useUserStore } from '../store/user';

/**
 * 创建班级队列
 */
export async function setupClassQueue(classId: string | number) {
  try {
    const result = await request(`/create/${classId}`, {
      method: 'POST'
    });
    
    return result;
  } catch (error) {
    console.error('创建班级队列失败:', error);
    throw error;
  }
}

/**
 * 清理班级队列
 */
export async function cleanupClassQueue(classId: string | number) {
  try {
    const result = await request(`/class/${classId}`, {
      method: 'DELETE'
    });
    
    return result;
  } catch (error) {
    console.error('清理班级队列失败:', error);
    throw error;
  }
}

/**
 * 获取班级消息
 */
export async function getClassMessages() {
  try {
    const result = await request('/class/messages', {
      method: 'GET'
    });
    
    return result;
  } catch (error) {
    console.error('获取班级消息失败:', error);
    throw error;
  }
}

/**
 * 发送班级消息
 */
export async function sendClassMessage(message: {
  teacherId: number;
  teacherName: string;
  classId: number;
  content: string;
  priority: number;
}, problemIds?: number[]) {
  try {
    let url = '/send/message';
    
    // 如果有problemIds，添加到查询参数
    if (problemIds && problemIds.length > 0) {
      url += `?${problemIds.map(id => `problemIds=${id}`).join('&')}`;
    }
    
    const result = await request(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(message)
    });
    
    return result;
  } catch (error) {
    console.error('发送班级消息失败:', error);
    throw error;
  }
}

/**
 * 提交任务
 */
export async function commitTask(messageId: number) {
  try {
    const result = await request(`/task/commit?messageId=${messageId}`, {
      method: 'POST'
    });
    
    return result;
  } catch (error) {
    console.error('提交任务失败:', error);
    throw error;
  }
} 